let date = require('../../../utils/date.js');
let repair = require('../../../api/ca_repair.js');
let that = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: {},// 用户信息
        picker: ['台式', '笔记本'],// 电脑类型选择
        index: 0,// 电脑类型下标
        compute_name: '',// 电脑型号
        reason: '',// 原因
        date: '',// 日期
        time: '',// 时间
        imgList: [],// 图片列表
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        that = this;
        let user = wx.getStorageSync('user');
        that.setData({
            user: user,
            date: date.date(new Date()),
            time: date.time(new Date()),
        })

    },

    /**
     * 电脑类型修改事件 
     */
    PickerChange(e) {
        that.setData({
            index: e.detail.value
        })
    },

    /**
     * 选择图片按钮
     */
    ChooseImage() {
        wx.chooseImage({
            count: 4, //默认9
            success: (res) => {
                if (this.data.imgList.length != 0) {
                    this.setData({
                        imgList: this.data.imgList.concat(res.tempFilePaths)
                    })
                } else {
                    this.setData({
                        imgList: res.tempFilePaths
                    })
                }
            }
        });
    },

    /**
     * 图片查看按钮
     */
    ViewImage(e) {
        wx.previewImage({
            urls: this.data.imgList,
            current: e.currentTarget.dataset.url
        });
    },

    /**
     * 删除图片按钮
     */
    DelImg(e) {
        this.data.imgList.splice(e.currentTarget.dataset.index, 1);
        this.setData({
            imgList: this.data.imgList
        })
    },

    /**
     * 提交按钮事件
     */
    async submitRepairTabel() {
        let realName = that.data.user.realName;
        let dormNum = that.data.user.dormNum;
        let phone = that.data.user.phone;
        let compute_type = that.data.picker[that.data.index];
        let compute_name = that.data.compute_name;
        let reason = that.data.reason;
        let imgList = that.data.imgList;
        let submit_time = new Date();
        let time = that.data.date + " " + that.data.time + ":00";
        let order_time = new Date(time);
        let content = '用户名： ' + realName + '\n宿舍名：' + dormNum + '\n手机号码：' + phone + '\n'
            + '电脑类型：' + compute_type + '\n电脑型号：' + compute_name + '\n原因：' + reason + '\n预约时间：' + time;
        let data = {};
        // 判断是否有写下愿因
        if (reason == '' || reason == undefined) {
            wx.showToast({
                title: '请写下原因',
                icon: 'none',
                duration: 2000,
            })
        } else {
            // 信息确认框
            await wx.showModal({
                title: '维修表信息',
                content: content,
                async success(res) {
                    // 判断是否确定信息
                    if (res.confirm) {
                        wx.showLoading({
                            title: '维修提交中...',
                        })
                        data = {
                            compute_type: compute_type,
                            compute_name: compute_name,
                            reason: reason,
                            submit_time: submit_time,
                            order_time: order_time,
                        }// 传入数据库数据
                        // 判断是否有图片
                        if (imgList.length != 0) {
                            imgList = await repair.uploadFile(imgList).then(res => { return res; });// 上传图片
                            data.imgList = imgList;
                        }
                        let repair_tabel = await repair.repair_tebel_add(data).then(res => { return res; });
                        // 判断是否添加成功
                        if (repair_tabel.result.errMsg == "collection.add:ok") {
                            console.log('add complete!');
                            let tempPage = getCurrentPages(); // 当前页变量
                            let prevPage = tempPage[tempPage.length - 2]; // 上一页变量
                            prevPage.setData({
                                result: 'success',
                            })
                            await wx.hideLoading({
                                success: (res) => { },
                            })
                            wx.navigateBack({
                                delta: 1,
                            })
                        } else{
                            wx.hideLoading({
                                success: (res) => { },
                            })
                            console.log('add fail!');
                            wx.showToast({
                                title: '维修提交失败，重新提交',
                                icon: 'none',
                                duration: 2000,
                            })
                        }
                    }
                }
            })
        }
    },

    
    /**
     * 右上角分享
     */
    onShareAppMessage() {
        return {
            title: '首页',
            path: '/pages/CA/home/home',
            imageUrl: '../../../images/share.png'
        }
    },
})